{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-12T08:42:53.932466Z",
     "start_time": "2025-08-12T08:42:53.894274Z"
    }
   },
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'cv2'",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mModuleNotFoundError\u001B[39m                       Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[4]\u001B[39m\u001B[32m, line 2\u001B[39m\n\u001B[32m      1\u001B[39m \u001B[38;5;66;03m# 读取图像，绘图的工具包\u001B[39;00m\n\u001B[32m----> \u001B[39m\u001B[32m2\u001B[39m \u001B[38;5;28;01mimport\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[34;01mcv2\u001B[39;00m\n\u001B[32m      3\u001B[39m \u001B[38;5;28;01mimport\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[34;01mmatplotlib\u001B[39;00m\u001B[34;01m.\u001B[39;00m\u001B[34;01mpyplot\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;28;01mas\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[34;01mplt\u001B[39;00m\n\u001B[32m      4\u001B[39m \u001B[38;5;28;01mfrom\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[34;01mmatplotlib\u001B[39;00m\u001B[34;01m.\u001B[39;00m\u001B[34;01mpatches\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;28;01mimport\u001B[39;00m Rectangle\n",
      "\u001B[31mModuleNotFoundError\u001B[39m: No module named 'cv2'"
     ]
    }
   ],
   "source": [
    "# 读取图像，绘图的工具包\n",
    "import cv2\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.patches import Rectangle\n",
    "# yoloV3的预测器\n",
    "from core.predicter import Predictor\n",
    "\n",
    "# coco数据集中的类别信息\n",
    "classes = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', \n",
    "           'train', 'truck', 'boat', 'traffic light', 'fire hydrant', \n",
    "           'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog',\n",
    "           'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe',\n",
    "           'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', \n",
    "           'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', \n",
    "           'skateboard', 'surfboard','tennis racket', 'bottle', 'wine glass', 'cup', 'fork', \n",
    "           'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', \n",
    "           'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', \n",
    "           'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', \n",
    "           'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', \n",
    "           'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-12T08:42:20.010563Z",
     "start_time": "2025-08-12T08:42:19.967412Z"
    }
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'cv2' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mNameError\u001B[39m                                 Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[3]\u001B[39m\u001B[32m, line 2\u001B[39m\n\u001B[32m      1\u001B[39m \u001B[38;5;66;03m# 1.读取要进行目标检测的图像\u001B[39;00m\n\u001B[32m----> \u001B[39m\u001B[32m2\u001B[39m img = cv2.imread(\u001B[33m\"\u001B[39m\u001B[33mimage.jpg\u001B[39m\u001B[33m\"\u001B[39m)\n\u001B[32m      3\u001B[39m \u001B[38;5;66;03m# 2.实例化，并加载预训练模型\u001B[39;00m\n\u001B[32m      4\u001B[39m predictor = Predictor(class_num=\u001B[32m80\u001B[39m, yolov3=\u001B[33m\"\u001B[39m\u001B[33mweights/yolov3.h5\u001B[39m\u001B[33m\"\u001B[39m)\n",
      "\u001B[31mNameError\u001B[39m: name 'cv2' is not defined"
     ]
    }
   ],
   "source": [
    "# 1.读取要进行目标检测的图像\n",
    "img = cv2.imread(\"image.jpg\")\n",
    "# 2.实例化，并加载预训练模型\n",
    "predictor = Predictor(class_num=80, yolov3=\"weights/yolov3.h5\")\n",
    "# 3.获取检测结果\n",
    "boundings = predictor.predict(img)\n",
    "# 4.将检测结果绘制在图像上\n",
    "# 4.1 绘制图像\n",
    "plt.imshow(img[:,:,::-1])\n",
    "# 4.2 绘制检测结果\n",
    "# 获取坐标区域\n",
    "ax = plt.gca()\n",
    "# 4.2 遍历检测框，将检测框绘制在图像上\n",
    "for bounding in boundings:\n",
    "    # 绘制框\n",
    "    rect = Rectangle((bounding[0].numpy(), bounding[1].numpy()), bounding[2].numpy(\n",
    "    ) - bounding[0].numpy(), bounding[3].numpy()-bounding[1].numpy(), color='r', fill=False)\n",
    "    # 将框显示在图像上\n",
    "    ax.add_patch(rect)\n",
    "    # 显示类别信息\n",
    "    # 获取类别信息的id\n",
    "    label_id = bounding[5].numpy().astype('int32')\n",
    "    # 获取类别\n",
    "    label = classes[label_id]\n",
    "    # 将标注信息添加在图像上\n",
    "    ax.text(bounding[0].numpy(), bounding[1].numpy() + 8,\n",
    "            label, color='w', size=11, backgroundcolor=\"none\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
